package lab.dexter.bbsreader.utils;

import lab.dexter.bbsreader.R;
import lab.dexter.bbsreader.R.id;
import lab.dexter.bbsreader.R.layout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AccountEditor extends Activity {
	
	public static final String PARAM_USERNAME = "USERNAME";
	public static final String PARAM_PASSWORD = "PASSWORD";
	
	EditText mEditAccountName;
	EditText mEditPassword;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = this.getIntent();
		
		this.setContentView(R.layout.account_editor);

	    mEditAccountName = (EditText) findViewById(R.id.edit_account_name);
	    mEditPassword = (EditText) findViewById(R.id.edit_password);
	    
	    Button okButton = (Button) findViewById(R.id.button_ok);
	    Button cancelButton = (Button) findViewById(R.id.button_cancel);
	    Button deleteButton = (Button) findViewById(R.id.button_delete);
		
		String user = "";
		
		if(intent.getAction().equals(Intent.ACTION_EDIT)) {
			user = intent.getStringExtra(PARAM_USERNAME);
			this.setTitle("Edit Account");
			mEditAccountName.setEnabled(false);
		}
		else {
			// intent.getAction().equals(Intent.ACTION_INSERT)
			this.setTitle("Add Account");
			deleteButton.setEnabled(false);
		}
		
	    
	    mEditAccountName.setText(user);
	    
	    
	    okButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
		         Intent mIntent = new Intent(Intent.ACTION_EDIT);
		         mIntent.putExtra(PARAM_USERNAME, mEditAccountName.getText().toString());
		         mIntent.putExtra(PARAM_PASSWORD, mEditPassword.getText().toString());
		         setResult(RESULT_OK, mIntent);
		         finish();
			}
			
	    });
	    
	    cancelButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				setResult(RESULT_CANCELED, null);
				finish();
			}
	    });
	    
	    deleteButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				Intent mIntent = new Intent(Intent.ACTION_DELETE);
				mIntent.putExtra(PARAM_USERNAME, mEditAccountName.getText().toString());
				setResult(RESULT_OK, mIntent);
				finish();
			}
	    	
	    });
	}

}
